package com.xzqwjw.taskmanager.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.leaderinfo.model.CategoryUnit;
import com.leaderinfo.model.InfoUnit;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class UnitDao implements com.leaderinfo.service.OperateDatabaseService {

	private SQLiteDatabase objDB;
	private String strDBFile;

	public UnitDao(String strDBFile) {
		this.strDBFile = strDBFile;
	}

	@Override
	public Map<String, Object> getRecordByAID(int intAID) {
		InfoUnit objUnit = null;
		Map<String, Object> mapInfo = null;

		objDB = SQLiteDatabase.openOrCreateDatabase(strDBFile, null);

		String strSQL = "SELECT * FROM info_cadre_unit WHERE aid=?";
		String[] aryArgs = { String.valueOf(intAID) };

		Cursor objCursor = objDB.rawQuery(strSQL, aryArgs);
		if (objCursor.moveToFirst()) {
			objUnit = new InfoUnit();
			getInfo(objCursor, objUnit);
			mapInfo = new HashMap<String, Object>();
			mapInfo.put("UnitInfo", objUnit);
		}
		objCursor.close();
		objDB.close();
		return mapInfo;
	}

	@Override
	public Map<String, Object> getRecordByBasicID(int intBasicID) {
		InfoUnit objUnit = null;
		Map<String, Object> mapInfo = null;

		objDB = SQLiteDatabase.openOrCreateDatabase(strDBFile, null);

		String strSQL = "SELECT * FROM info_cadre_unit WHERE id_basic=?";
		String[] aryArgs = { String.valueOf(intBasicID) };

		Cursor objCursor = objDB.rawQuery(strSQL, aryArgs);
		if (objCursor.moveToFirst()) {
			objUnit = new InfoUnit();
			getInfo(objCursor, objUnit);
			mapInfo = new HashMap<String, Object>();
			mapInfo.put("UnitInfo", objUnit);
		}
		objCursor.close();
		objDB.close();
		return mapInfo;
	}

	@Override
	public List<Map<String, Object>> getRecordsByBasicID(int intBasicID) {
		List<Map<String, Object>> listInfo = new ArrayList<Map<String, Object>>();
		Map<String, Object> mapInfo = null;
		InfoUnit objUnit = null;

		objDB = SQLiteDatabase.openOrCreateDatabase(strDBFile, null);

		String strSQL = "SELECT * FROM info_cadre_unit WHERE id_basic=? ORDER BY aid DESC";
		String[] aryArgs = { String.valueOf(intBasicID) };

		Cursor objCursor = objDB.rawQuery(strSQL, aryArgs);
		while (objCursor.moveToNext()) {
			mapInfo = new HashMap<String, Object>();
			objUnit = new InfoUnit();
			getInfo(objCursor, objUnit);
			mapInfo.put("UnitInfo", objUnit);
			listInfo.add(mapInfo);
		}
		objCursor.close();
		objDB.close();
		return listInfo;
	}

	@Override
	public List<Map<String, Object>> getAllRecords() {
		return null;
	}

	private void getInfo(Cursor objCursor, InfoUnit objUnit) {
		objUnit.AID = objCursor.getInt(objCursor.getColumnIndex("aid"));
		objUnit.BasicID = objCursor.getInt(objCursor.getColumnIndex("id_basic"));
		objUnit.PositionCategoryID = objCursor.getInt(objCursor.getColumnIndex("id_position_category"));
		objUnit.UnitID = objCursor.getInt(objCursor.getColumnIndex("id_unit"));
		objUnit.JobName = objCursor.getString(objCursor.getColumnIndex("job_name"));
		objUnit.JobLevelID = objCursor.getInt(objCursor.getColumnIndex("id_job_level"));
		objUnit.JobCategoryID = objCursor.getInt(objCursor.getColumnIndex("id_job_category"));
		objUnit.WorkContent = objCursor.getString(objCursor.getColumnIndex("work_content"));
		objUnit.JobChangeCategoryID = objCursor.getInt(objCursor.getColumnIndex("id_job_change_category"));
		objUnit.HoldpostDate = objCursor.getString(objCursor.getColumnIndex("holdpost_date"));
		objUnit.HoldpostReferenceNumber = objCursor.getString(objCursor.getColumnIndex("holdpost_reference_number"));
		objUnit.AppointmentCategory = objCursor.getString(objCursor.getColumnIndex("appointment_category"));

		objUnit.NowHoldpostDate = objCursor.getString(objCursor.getColumnIndex("now_holdpost_date"));
		objUnit.JobType = objCursor.getString(objCursor.getColumnIndex("job_type"));
		objUnit.IsTeamMember = objCursor.getString(objCursor.getColumnIndex("is_team_member"));
		objUnit.HoldpostState = objCursor.getString(objCursor.getColumnIndex("holdpost_state"));
		objUnit.DepositionType = objCursor.getString(objCursor.getColumnIndex("deposition_type"));
		objUnit.DepositionReferenceNumber = objCursor
				.getString(objCursor.getColumnIndex("deposition_reference_number"));
		objUnit.DepositionDate = objCursor.getString(objCursor.getColumnIndex("deposition_date"));
		objUnit.IsCommunicating = objCursor.getString(objCursor.getColumnIndex("is_communicating"));
		objUnit.CommunicatingReason = objCursor.getString(objCursor.getColumnIndex("communicating_reason"));
		objUnit.CommunicatingWay = objCursor.getString(objCursor.getColumnIndex("communicating_way"));
		objUnit.ExchangeTo = objCursor.getString(objCursor.getColumnIndex("exchange_to"));
		objUnit.LeaderOrder = objCursor.getInt(objCursor.getColumnIndex("leader_order"));
	}

	public String getUnitJob(int intBasicID) {
		String strReturn = "";
		String strJobName = "";
		int intUnitID = 0;
		String strUnitCategory = "";
		Map<String, Object> mapUnitCategory = null;
		UnitCategoryDao objUnitCategoryDAO = null;
		CategoryUnit objUnitCategory = null;
		InfoUnit objUnit = null;

		Map<String, Object> mapInfoUnit = null;
		List<Map<String, Object>> listInfoUnit = getRecordsByBasicID(intBasicID);

		if (listInfoUnit != null) {
			for (int i = 0; i < listInfoUnit.size(); i++) {
				mapInfoUnit = listInfoUnit.get(i);
				objUnit = (InfoUnit) mapInfoUnit.get("UnitInfo");

				intUnitID = objUnit.UnitID;
				if (intUnitID > 0) {
					strJobName = objUnit.JobName;

					objUnitCategoryDAO = new DAOUnitCategory(this.strDBFile);
					mapUnitCategory = objUnitCategoryDAO.getRecordByAID(intUnitID);
					objUnitCategory = (CategoryUnit) mapUnitCategory.get("UnitCategory");
					strUnitCategory = objUnitCategory.UnitName;
					if (strUnitCategory.startsWith("������")) {
						strUnitCategory = strUnitCategory.substring(3);
					}

					// ȥ����ͷ���й�����4����
					if (strUnitCategory.startsWith("�й�����")) {
						strUnitCategory = strUnitCategory.substring(4);
					}

					// ȥ����ͷ������2����
					if (strUnitCategory.startsWith("����")) {
						strUnitCategory = strUnitCategory.substring(2);
					}

					strReturn = strUnitCategory + strJobName;
				} else {
					strReturn = objUnit.JobName;
				}

			}
		}
		return strReturn;
	}

	@Override
	public List<Map<String, Object>> getRecordsByWhere(String strWhere) {
		return null;
	}
}
